package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.dto.SaveShopDto;
import com.example.pojo.Shop;
import com.example.service.ShopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/shops")
public class ShopController {
    private final ShopService shopService;

    @GetMapping("/getAllShops")
    public Result<List<Shop>> getAllShops() {
        List<Shop> shops = shopService.getAllShops();
        return new Result<>(shops, 1, "获取总店成功");
    }

    @PostMapping("/saveShop")
    public Result<Object> saveShop(@RequestBody SaveShopDto saveShopDto) {
        Shop shop = new Shop();
        shop.setPcode("0");
        shop.setIsDelete(0);
        BeanUtil.copyProperties(saveShopDto, shop);
        Boolean flag = shopService.saveShop(shop);
        Integer code = flag ? 1 : 0;
        String message = flag ? "新增成功" : "新增失败, 店编号已存在";
        return new Result<>(null, code, message);
    }

    @GetMapping("/getShop")
    public Result<List<Shop>> getShop(@RequestParam String name, @RequestParam String code) {
        List<Shop> shops = shopService.getShop(name, code);
        return new Result<>(shops, 1, "查询成功");
    }
}
